package cn.itcast.bos.service.base.impl;

import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.hibernate.mapping.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.CourierResponsity;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.CourierService;
@Service
@Transactional
public class CourierServiceImpl implements CourierService {

	@Autowired
	//注入dao层
	private CourierResponsity courierResponsity;
	
	@Override
	public void save(Courier courier) {
		courierResponsity.save(courier);
	}

	@Override
	public Page findPageData(Specification<Courier> specification,  Pageable pageable) {
		Page<Courier> page = courierResponsity.findAll(specification,pageable);
		return page;
	}

	@Override
	public void delBatch(String[] idArray) {
		for (int i = 0; i < idArray.length; i++) {
			Integer id=Integer.parseInt(idArray[i]);
			courierResponsity.delBatch(id);
		}
	}

	@Override
	public List<Courier> findnoassociation() {
		//使用specification的方法
		Specification<Courier> specification=new Specification<Courier>() {
			@Override
			public Predicate toPredicate(Root<Courier> root, 
					CriteriaQuery<?> query,
					CriteriaBuilder cb) {
				Predicate p=cb.isEmpty(root.get("fixedAreas").as(Set.class));
				return p;
			}
		}; 
		return courierResponsity.findAll(specification);
	}
}
